#!/bin/bash

set -o errexit
set -o xtrace

test_default_replicas_service() {
	kpatch_delete_field pxc $1 "/spec/haproxy/replicasServiceEnabled" && sleep 1
	compare_kubectl "service/$1-haproxy-replicas"
}

test_disable_replicas_service() {
	kpatch_set_field pxc $1 "/spec/haproxy/replicasServiceEnabled" false && sleep 1
	wait_for_delete "svc/$1-haproxy-replicas"
	(kubectl_bin get svc "$1-haproxy-replicas" || :) 2>&1 | grep -e "not found$" >/dev/null
}

test_enable_replicas_service() {
	kpatch_set_field pxc $1 "/spec/haproxy/replicasServiceEnabled" true && sleep 1
	compare_kubectl "service/$1-haproxy-replicas"
}

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

check_haproxy_writer() {
	for i in $(seq 0 2); do
		local haproxy_pod_ip=$(kubectl_bin get pods $cluster-haproxy-$i -o jsonpath='{.status.podIP}')
		run_mysql "SHOW VARIABLES LIKE 'server_id'" "-h $haproxy_pod_ip -uroot -proot_password" >"/$tmp_dir/server_id_$i.sql"
	done
	for i in $(seq 0 1); do
		diff -u "/$tmp_dir/server_id_$i.sql" "/$tmp_dir/server_id_$((i + 1)).sql"
	done
}

main() {
	create_infra $namespace

	if version_gt "1.19" && [ $EKS -ne 1 ]; then
		apply_config "$test_dir/conf/container-rc.yaml"
	else
		apply_config "$test_dir/conf/docker-rc.yaml"
	fi

	desc 'create first PXC cluster with HAProxy'
	cluster="haproxy"
	spinup_pxc "$cluster" "$test_dir/conf/$cluster.yml" 3 10

	desc 'checking all haproxy pods point to the same writer'
	wait_for_running "$cluster-pxc" 3
	check_haproxy_writer

	desc 'delete active writer and checking all haproxy pods still point to the same writer'
	kubectl_bin delete pod $cluster-pxc-0
	check_haproxy_writer

	desc 'check advanced options are enabled in haproxy statefulset'
	compare_kubectl pdb/$cluster-haproxy
	compare_kubectl statefulset/$cluster-haproxy

	desc 'default haproxy-replicas service'
	test_default_replicas_service $cluster

	desc 'disable haproxy-replicas service'
	test_disable_replicas_service $cluster

	desc 'enable haproxy-replicas service'
	test_enable_replicas_service $cluster

	desc 'enable proxy-sql'
	apply_config "$test_dir/conf/$cluster-proxysql.yml"
	wait_for_running "$cluster-proxysql" 2
	compare_kubectl statefulset/$cluster-proxysql
	compare_kubectl service/$cluster-proxysql

	compare_mysql_cmd_local "ping_timeout_server" \
		"SELECT * FROM global_variables WHERE variable_name = '\''mysql-ping_timeout_server'\'';" \
		"-h127.0.0.1 -P6032 -uproxyadmin -padmin_password" "$cluster-proxysql-0" "" -c'proxysql'
	apply_config "$test_dir/conf/config-secret-proxysql.yaml"
	wait_cluster_consistency "$cluster" 3 2
	compare_kubectl statefulset/$cluster-proxysql "-secret"
	compare_mysql_cmd_local "ping_timeout_server" \
		"SELECT * FROM global_variables WHERE variable_name = '\''mysql-ping_timeout_server'\'';" \
		"-h127.0.0.1 -P6032 -uproxyadmin -padmin_password" "$cluster-proxysql-0" "-secret" -c'proxysql'
	wait_cluster_consistency "$cluster" 3 2

	desc 're-enable haproxy'
	apply_config "$test_dir/conf/$cluster.yml"
	wait_for_running "$cluster-haproxy" 3
	compare_kubectl statefulset/$cluster-haproxy
	compare_kubectl service/$cluster-haproxy
	compare_kubectl pdb/$cluster-haproxy
	kubectl_bin exec haproxy-haproxy-0 -c haproxy -it -- bash -c 'echo "show info" | socat stdio unix-connect:/etc/haproxy/pxc/haproxy.sock' \
		| grep "Maxconn:" >"$tmp_dir"/haproxy_maxconn.txt
	diff --strip-trailing-cr "$tmp_dir"/haproxy_maxconn.txt "$test_dir"/compare/haproxy_maxconn.txt

	apply_config "$test_dir/conf/config-secret-haproxy.yaml"
	wait_cluster_consistency "$cluster" 3 3
	compare_kubectl statefulset/$cluster-haproxy "-secret"
	kubectl_bin exec haproxy-haproxy-0 -c haproxy -it -- bash -c 'echo "show info" | socat stdio unix-connect:/etc/haproxy/pxc/haproxy.sock' \
		| grep "Maxconn:" >"$tmp_dir"/haproxy_maxconn.txt
	diff --strip-trailing-cr "$tmp_dir"/haproxy_maxconn.txt "$test_dir"/compare/haproxy_maxconn-secret.txt
	desc 'clean up'
	destroy $namespace
}

main
